package com.example.hzcccc.weather;

import android.content.DialogInterface;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;


public class Main2Activity extends AppCompatActivity {
    ListView listview;
    Cursor cursor;
    List<Map<String, Object>> list;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);


        listview = (ListView) findViewById(R.id.add_listview);
        Toolbar toolbar = (Toolbar) findViewById(R.id.id_add_toolbar);
        setSupportActionBar(toolbar);
        final ActionBar ab = getSupportActionBar();
        ab.setDisplayHomeAsUpEnabled(true);
        ab.setHomeButtonEnabled(true);

        //  initdatabase();

        initlistview();

    }

    private void initdatabase() {
//        SQLiteDatabase managerdatabase = this.openOrCreateDatabase("mdatabase",MODE_PRIVATE,null);
//        managerdatabase.execSQL("create table mdatabase(_id integer primary key autoincrement,name text,weather text,code integer)");
        SQLdm sqLdm = new SQLdm();
        SQLiteDatabase managerdatabase = sqLdm.openDatabase(Main2Activity.this);
        cursor = managerdatabase.rawQuery("select * from managercity", null);
        cursor.moveToFirst();

        String s = cursor.getString(0);
//            Toast.makeText(Main2Activity.this, s, Toast.LENGTH_SHORT).show();


    }

    private void initlistview() {
        final SimpleAdapter adapter = new SimpleAdapter(this, getdata(), R.layout.add_item,
                new String[]{"add_city", "add_weather"}, new int[]{R.id.add_item_city, R.id.add_item_weather});
        listview.setAdapter(adapter);
        listview.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, final int position, long id) {
                new AlertDialog.Builder(Main2Activity.this)
                        .setTitle(null)
                        .setMessage("是否删除该城市")
                        .setNegativeButton("不用了", null)
                        .setPositiveButton("是的", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {

                                Map<String, Object> map = list.get(position);
                                Object obj = map.get("add_city");
                                String s1 = obj.toString();

                                SQLdm s = new SQLdm();
                                SQLiteDatabase db = s.openDatabase(Main2Activity.this);
                                db.execSQL("DELETE FROM managercity WHERE name = '"+s1+"'");

                                list.remove(position);
                                adapter.notifyDataSetChanged();
                            }
                        }).show();

                return true;
            }
        });
        listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                //Toast.makeText(Main2Activity.this,"click",Toast.LENGTH_SHORT).show();
                new AlertDialog.Builder(Main2Activity.this)
                        .setTitle(null)
                        .setMessage("是否设置该城市为常用城市")
                        .setNegativeButton("不用了", null)
                        .setPositiveButton("是的", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                Toast.makeText(Main2Activity.this, "yes", Toast.LENGTH_SHORT).show();
                            }
                        }).show();
            }
        });
    }


    private List<Map<String, Object>> getdata() {
        SQLdm s = new SQLdm();
        SQLiteDatabase db = s.openDatabase(Main2Activity.this);
        Cursor cursor = db.rawQuery("SELECT * FROM managercity", null);
        list = new ArrayList<Map<String, Object>>();
        if (cursor.moveToFirst()) {
            do {
                String name = cursor.getString(0);
                String curtemp = cursor.getString(16);

                Map<String, Object> map = new HashMap<String, Object>();
                map.put("add_city", name);
                map.put("add_weather", curtemp);
                list.add(map);
            } while (cursor.moveToNext());
        }

        return list;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                Main2Activity.this.finish();
                break;
        }
        return super.onOptionsItemSelected(item);
    }
}
